package com.aaa.controller.sys;

import com.aaa.pojo.Dept;
import com.aaa.pojo.ResultBean;
import com.aaa.service.sys.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("/pagination")
    public ResultBean findAll(String page,String limit){
        List<Dept> deptList = deptService.findAll(page,limit);
        int count = deptService.findCount();
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(0);
        resultBean.setMsg("");
        resultBean.setCount(count);
        resultBean.setData(deptList);
        return resultBean;

    }

    @RequestMapping("/insert")
    public ResultBean insert(Dept dept){
        System.out.println(dept);
        dept.setCreateTime(new Date());
        int insert = deptService.insert(dept);

        ResultBean resultBean = new ResultBean();
        if (insert !=0){
            resultBean.setCode(0);
           resultBean.setMsg("");
        }else {
            resultBean.setCode(1);
            resultBean.setMsg("");
        }

        resultBean.setData(dept);
        return resultBean;
    }

    @RequestMapping("/delete")
    public ResultBean delete(@RequestParam("idList") List<String> idList){
        System.out.println(idList);
        int delete = deptService.delete(idList);
        ResultBean resultBean = new ResultBean();
        if(delete != 0){
            resultBean.setCode(0);
            resultBean.setMsg("");
        }else {
            resultBean.setCode(1);
            resultBean.setMsg("");
        }

        return resultBean;
    }

    @RequestMapping("/update")
    public ResultBean update(Dept dept){
        System.out.println(dept);
        dept.setUpdateTime(new Date());
        int update = deptService.update(dept);
        ResultBean resultBean = new ResultBean();

        if(update != 0){
            resultBean.setCode(0);
            resultBean.setMsg("");
        }else {
            resultBean.setCode(1);
            resultBean.setMsg("");
        }

        return resultBean;
    }

    @RequestMapping("/findIf")
    public ResultBean findIf(String deptName,String createBy,String updateBy,String page,String limit){

        List<Dept> anIf = deptService.findIf(deptName, createBy, updateBy, page, limit);
        int ifCount = deptService.findIfCount(deptName, createBy, updateBy, page, limit);

        ResultBean resultBean = new ResultBean();
        resultBean.setCount(0);
        resultBean.setMsg("");
        resultBean.setCount(ifCount);
        resultBean.setData(anIf);
        return resultBean;

    }
}
